import dataPreferences from '@ohos.data.preferences';
import HashMap from '@ohos.util.HashMap';
export class DataPreferences {
    constructor() {
        this.mDefaultName = "harmonyPreferences";
        this.mPreferencesMap = new HashMap();
        this.mIsLoadPreferences = false;
        this.mIsFlush = true;
    }
    static getInstance() {
        if (!DataPreferences.mDataPreferences) {
            DataPreferences.mDataPreferences = new DataPreferences();
        }
        return DataPreferences.mDataPreferences;
    }
    init(i17, j17) {
        let k17 = [this.mDefaultName];
        if (j17 != undefined) {
            k17 = k17.concat(j17);
        }
        for (let l17 = 0; l17 < k17.length; l17++) {
            this.initPreferences(i17, k17[l17]);
        }
        return this;
    }
    setFlush(h17) {
        this.mIsFlush = h17;
        return this;
    }
    initPreferences(b17, c17) {
        let d17 = { name: c17 };
        dataPreferences.getPreferences(b17, d17, (f17, g17) => {
            if (f17) {
                console.error("Failed to get preferences. code =" + f17.code + ", message =" + f17.message);
                return;
            }
            this.mPreferencesMap.set(c17, g17);
        });
    }
    setPreferences(a17) {
        this.mPreferences = this.mPreferencesMap.get(a17 == undefined ? this.mDefaultName : a17);
        if (a17 != undefined) {
            this.mIsLoadPreferences = true;
        }
        return this;
    }
    getPreferences() {
        return this.mPreferences;
    }
    put(v16, w16, x16) {
        if (!this.mIsLoadPreferences) {
            this.setPreferences();
        }
        this.mPreferences?.put(v16, w16, (z16) => {
            if (z16) {
                if (x16 != null) {
                    x16(false, z16);
                }
                this.mIsLoadPreferences = false;
                return;
            }
            if (this.mIsFlush) {
                this.flush();
            }
            if (x16 != null) {
                x16(true);
            }
            this.mIsLoadPreferences = false;
        });
    }
    putPromise(o16, p16, q16) {
        if (!this.mIsLoadPreferences) {
            this.setPreferences();
        }
        let r16 = this.mPreferences?.put(o16, p16);
        r16?.then(() => {
            if (this.mIsFlush) {
                this.flushPromise();
            }
            if (q16 != null) {
                q16(true);
            }
            this.mIsLoadPreferences = false;
        }).catch((u16) => {
            if (q16 != null) {
                q16(false, u16);
                this.mIsLoadPreferences = false;
            }
        });
    }
    putSync(m16, n16) {
        if (!this.mIsLoadPreferences) {
            this.setPreferences();
        }
        if (this.mIsFlush) {
            this.flushPromise();
        }
        this.mPreferences?.putSync(m16, n16);
        this.mIsLoadPreferences = false;
    }
    isPreferencesEmpty() {
        if (!this.mIsLoadPreferences) {
            this.setPreferences();
        }
        this.mIsLoadPreferences = false;
    }
    getSync(j16, k16) {
        this.isPreferencesEmpty();
        let l16 = this.mPreferences?.getSync(j16, k16);
        return l16;
    }
    getPromise(g16, h16) {
        this.isPreferencesEmpty();
        let i16 = this.mPreferences?.get(g16, h16);
        return i16;
    }
    get(e16, f16) {
        this.mTempKey = e16;
        this.mTempDataDefault = f16;
        return this;
    }
    result(z15, a16) {
        this.isPreferencesEmpty();
        this.mPreferences?.get(this.mTempKey, this.mTempDataDefault, (c16, d16) => {
            if (c16) {
                if (a16 != undefined) {
                    a16(c16);
                }
                return;
            }
            if (z15 != undefined) {
                z15(d16);
            }
        });
    }
    hasSync(y15) {
        this.isPreferencesEmpty();
        return this.mPreferences.hasSync(y15);
    }
    hasPromise(x15) {
        this.isPreferencesEmpty();
        return this.mPreferences.has(x15);
    }
    has(s15, t15) {
        this.isPreferencesEmpty();
        this.mPreferences?.has(s15, (v15, w15) => {
            if (v15) {
                t15(false);
                return;
            }
            if (w15) {
                t15(true);
            }
            else {
                t15(false);
            }
        });
    }
    deleteSync(r15) {
        this.isPreferencesEmpty();
        this.mPreferences.deleteSync(r15);
    }
    deletePromise(q15) {
        this.isPreferencesEmpty();
        return this.mPreferences.delete(q15);
    }
    delete(m15, n15) {
        this.isPreferencesEmpty();
        this.mPreferences?.delete(m15, (p15) => {
            if (p15) {
                if (n15 != undefined) {
                    n15(false);
                }
                return;
            }
            if (n15 != undefined) {
                n15(true);
            }
        });
    }
    getAllSync() {
        this.isPreferencesEmpty();
        let k15 = this.mPreferences.getAllSync();
        let l15 = this.getObjKeys(k15);
        return new AllPreferences(k15, l15);
    }
    getAllPromise(d15) {
        this.isPreferencesEmpty();
        let e15 = this.mPreferences.getAll();
        e15.then((i15) => {
            let j15 = this.getObjKeys(i15);
            d15(new AllPreferences(i15, j15));
        }).catch((h15) => {
            d15(undefined);
        });
    }
    getAll(y14) {
        this.isPreferencesEmpty();
        this.mPreferences?.getAll((a15, b15) => {
            if (a15) {
                y14(undefined);
                return;
            }
            let c15 = this.getObjKeys(b15);
            y14(new AllPreferences(b15, c15));
        });
    }
    getObjKeys(w14) {
        let x14 = Object.keys(w14);
        return x14;
    }
    flush(t14) {
        this.isPreferencesEmpty();
        this.mIsFlush = true;
        this.mPreferences.flush((v14) => {
            if (v14) {
                if (t14 != undefined) {
                    t14(false);
                }
                return;
            }
            if (t14 != undefined) {
                t14(true);
            }
        });
    }
    flushPromise() {
        this.isPreferencesEmpty();
        return this.mPreferences.flush();
    }
    clear(p14, q14) {
        this.isPreferencesEmpty();
        this.mPreferences?.clear((s14) => {
            if (s14) {
                if (q14 != undefined) {
                    q14(s14);
                }
                return;
            }
            if (p14 != undefined) {
                p14();
            }
        });
    }
    clearPromise() {
        this.isPreferencesEmpty();
        return this.mPreferences.clear();
    }
    clearSync() {
        this.isPreferencesEmpty();
        this.mPreferences.clearSync();
    }
    onChange(o14) {
        this.isPreferencesEmpty();
        this.mPreferences.on('change', o14);
    }
    onChangeOff(n14) {
        this.isPreferencesEmpty();
        this.mPreferences?.off('change', n14);
    }
    onMultiProcessChange(m14) {
        this.isPreferencesEmpty();
        this.mPreferences?.on('multiProcessChange', m14);
    }
    onMultiProcessChangeOff(l14) {
        this.isPreferencesEmpty();
        this.mPreferences?.off('multiProcessChange', l14);
    }
}
export class AllPreferences {
    constructor(j14, k14) {
        this.value = j14;
        this.allKeys = k14;
    }
}
